package com.ihelp.rest.mis.platform.service;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ihelp.rest.base.po.SysCode;
import com.ihelp.rest.mis.platform.dao.UserDao;
import com.ihelp.rest.mis.pojo.SysUser;
import com.ihelp.rest.utils.JsonCommon;
import com.ihelp.rest.utils.MethodUtil;
import com.ihelp.rest.utils.SessionUtils;

@Service
public class UserService {

	@Autowired
	private UserDao userDao;
	
	public String findUserPage(int start, int pageSize) {
		return JsonCommon.getGridJson(this.userDao.countAll(),
				this.userDao.listAll(start, pageSize)).toString();
	}
	
	/**
	 * 查询所有用户JSON 数据
	 * @return
	 */
	public String findAllUser() {
		return JSONArray.fromObject(this.userDao.querySysUser()).toString();
	}
	
	/**
	 * 获取用户
	 * @param uuid
	 * @return
	 */
	public SysUser getUser(String uuid){
		if(StringUtils.isBlank(uuid)){
			return null;
		}
		return this.userDao.get(uuid);
	}
	
	/**
	 * 登陆验证
	 * @return
	 */
	public String loginUser(HttpServletRequest request, SysUser user){
		Map<String, String> result = new HashMap<String, String>();
		SysUser dbUser = this.userDao.querySysUser(user.getLoginName());
		if(dbUser == null){
			result.put("data", "用户不存在");
		}
		else if(StringUtils.isBlank(dbUser.getLoginPassword()) || StringUtils.isBlank(user.getLoginPassword())){
			result.put("data", "密码错误");
		}//密码
		else if(!dbUser.getLoginPassword().equals(MethodUtil.MD5(user.getLoginPassword()))){
			result.put("data", "密码输入不正确");
		}//验证码
		else if(SessionUtils.getValidateCode(request) == null || StringUtils.isBlank(user.getValidateCode()) || 
				!SessionUtils.getValidateCode(request).equals(user.getValidateCode().toLowerCase())){
			result.put("data", "验证码输入不正确");
		}//用户禁用
		else if(dbUser.getStatus() == 0){
			result.put("data", "当前用户被禁用，无法登陆");
		}//用户合法
		else{
			result.put("data", SysCode.SUCCESS);
			//加入Session管理
			SessionUtils.setUser(request, dbUser);
		}
		return JSONObject.fromObject(result).toString();
	}
	
	/**
	 * 添加用户
	 * @return
	 */
	public String addUser(SysUser user) {
		if (StringUtils.isBlank(user.getUuid())) {
			// 第一次保存, AJAX提交form UUID未空串
			user.setUuid(null);
			user.setType(1);//用户级
			user.setLoginPassword(MethodUtil.MD5(user.getLoginPassword()));
			SysUser oldUser = this.userDao.querySysUser(user.getLoginName());
			if(oldUser != null){
				//用户名已存在
				return SysCode.FAIL;
			}
		}
		this.userDao.saveOrUpdate(user);
		return SysCode.SUCCESS;
	}
	
	/**
	 * 删除用户
	 */
	public String delUser(SysUser user) {
		Map<String ,Object >  result = new HashMap<String, Object>();
		int i = this.getSysUserCnt(user.getUuid());
		if(i>0){
			result.put("success", "false");
			result.put("data", "此会员不可删除！");
		}else{
			this.userDao.deleteObject(user);
			result.put("success", "true");
			result.put("data", SysCode.SUCCESS);
		}
		return JSONObject.fromObject(result).toString() ;
	}
	
	/**
	 * 用户密码初始化
	 * @param user
	 * @return
	 */
	public String updateUserPass(String uuid) {
		SysUser user = this.userDao.get(uuid);
		user.setLoginPassword(MethodUtil.MD5(SysCode.INITPASSWORD));
		this.userDao.update(user);
		return SysCode.INITPASSWORD;
	}
	
	/**
	 * 用户密码修改
	 * @param currentPassword
	 * @param newPassword
	 * @return
	 */
	public String updateUserPass(HttpServletRequest request, String currentPassword, String newPassword){
		//session 中获取用户信息
		SysUser user = SessionUtils.getUser(request);
		if(user != null && MethodUtil.MD5(currentPassword).equals(user.getLoginPassword())){
			user.setLoginPassword(MethodUtil.MD5(newPassword));
			this.userDao.update(user);
			//用户session 重置
			SessionUtils.setUser(request, user);
			return SysCode.SUCCESS;
		}
		return SysCode.FAIL;
	}
	
	/**
	 * 查询系统超级用户
	 * @return
	 */
	public SysUser findSystemUser(){
		return userDao.querySystemUser(SysCode.SYSTEMUSER);
	}
	
	/**
	 * 添加系统超级用户 
	 */
	public void saveSystemUser(){
		SysUser sys = new SysUser();
		sys.setName("超级管理员");
		sys.setLoginName(SysCode.SYSTEMUSER);
		sys.setLoginPassword(MethodUtil.MD5(SysCode.INITPASSWORD));
		sys.setStatus(1);
		sys.setType(0);
		userDao.save(sys);
	}
	
	public int getSysUserCnt(String uuid){
		return this.userDao.querySysUserCnt(uuid);
	}
}
